# -*- mode: python -*-

Import(
    [
        "env",
        "have_sasl_lib",
        "http_client",
        "version_extra",
        "version_parts",
        "wiredtiger",
    ]
)

env = env.Clone()

# Ideally 's2' would be scoped narrowly but it is spammed in all over the place by
# db/geo unfortunately.
env.InjectThirdParty(
    libraries=[
        "s2",
    ]
)


env.SConscript(
    must_exist=1,
    dirs=[
        "admission",
        "auth",
        "catalog",
        "collection_crud",
        "commands",
        "concurrency",
        "exec",
        "fts",
        "ftdc",
        "geo",
        "process_health",
        "index",
        "matcher",
        "op_observer",
        "pipeline",
        "query",
        "repl",
        "s",
        "session",
        "sorter",
        "stats",
        "storage",
        "timeseries",
        "transaction",
        "ttl",
        "update",
        "views",
    ],
    exports=[
        "env",
    ],
)

env.CppUnitTest(
    target="server_base_test",
    source=[],
    LIBDEPS=[],
)

env.Benchmark(
    target="profile_filter_bm",
    source=[
        "profile_filter_bm.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/db/commands/profile_common",
        "$BUILD_DIR/mongo/util/processinfo",
        "profile_settings",
    ],
    CONSOLIDATED_TARGET="query_bm",
)

env.CppUnitTest(
    target="profile_filter_test",
    source=[
        "profile_filter_test.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/db/commands/profile_common",
        "$BUILD_DIR/mongo/db/service_context_test_fixture",
    ],
)

env.CppUnitTest(
    target="profile_settings_test",
    source=[],
    LIBDEPS=[],
)

env.CppUnitTest(
    target="change_stream_pre_images_manager_test",
    source=[],
    LIBDEPS=[],
)

env.CppUnitTest(
    target="default_baton_test",
    source=[],
    LIBDEPS=[],
    PROVE_ALIASES=["server-programmability"],
)

env.CppUnitTest(
    target="prepare_conflict_tracker_test",
    source=[],
    LIBDEPS=[],
)

env.CppUnitTest(
    target="client_out_of_line_executor_test",
    source=[],
    LIBDEPS=[],
    PROVE_ALIASES=["server-programmability"],
)

env.CppUnitTest(
    target="mongod_options_test",
    source=[],
    LIBDEPS=[],
    PROVE_ALIASES=["server-programmability"],
)

env.CppUnitTest(
    target="replica_set_endpoint_test",
    source=[
        "direct_shard_client_tracker_test.cpp",
        "replica_set_endpoint_sharding_state_test.cpp",
        "replica_set_endpoint_test_fixture.cpp",
        "replica_set_endpoint_util_test.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/rpc/message",
        "$BUILD_DIR/mongo/transport/transport_layer_mock",
        "auth/authmocks",
        "commands",
        "commands_test_example",
        "repl/replmocks",
        "s/sharding_catalog_manager",
        "service_context_d_test_fixture",
        "shard_role_api",
    ],
)

asioEnv = env.Clone()
asioEnv.InjectThirdParty("asio")

env.BazelProgram(
    target="mongotrafficreader",
    source=[],
    LIBDEPS=[],
)

if env.TargetOSIs("windows"):
    generatedDbManifest = env.Substfile(
        "mongod.manifest.in",
        SUBST_DICT=[
            ("@mongo_version_major@", version_parts[0]),
            ("@mongo_version_minor@", version_parts[1]),
            ("@mongo_version_patch@", version_parts[2]),
            ("@mongo_version_extra@", version_parts[3]),
            ("@mongo_version_extra_str@", version_extra),
        ],
    )

    env.Alias("generated-sources", generatedDbManifest)
    env.Depends("mongod.res", generatedDbManifest)

env.BazelProgram(
    target="mongod",
    source=[],
    LIBDEPS=[],
    AIB_COMPONENT="mongod",
    AIB_COMPONENTS_EXTRA=[
        "core",
        "default",
        "devcore",
        "dist",
        "dist-test",
        "integration-tests",
        "serverless",
        "serverless-test",
        "servers",
    ],
)

envWithAsio = env.Clone()
envWithAsio.InjectThirdParty(libraries=["asio"])

if wiredtiger:
    envWithAsio.CppUnitTest(
        target="db_base_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="change_collection_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="command_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="fle_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="operation_test",
        source=[],
        LIBDEPS=[],
        PROVE_ALIASES=["server-programmability"],
    )

    envWithAsio.CppUnitTest(
        target="query_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="read_write_concern_test",
        source=[],
        LIBDEPS=[],
    )

    envWithAsio.CppUnitTest(
        target="session_test",
        source=[],
        LIBDEPS=[],
    )

env.CppUnitTest(
    target="shard_role_test",
    source=[],
    LIBDEPS=[],
)

env.CppUnitTest(
    target="vector_clock_test",
    source=[],
    LIBDEPS=[],
)

envWithAsio.CppUnitTest(
    target="db_unittest_with_config_server_test_fixture_test",
    source=[],
    LIBDEPS=[],
)

asioEnv.CppIntegrationTest(
    target="db_integration_test",
    source=[
        "nesting_depth_test.cpp",
        "$BUILD_DIR/mongo/db/query/client_cursor/exhaust_cursor_currentop_integration_test.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/client/clientdriver_network",
        "$BUILD_DIR/mongo/executor/network_interface_fixture",
        "$BUILD_DIR/mongo/rpc/message",
        "$BUILD_DIR/mongo/transport/transport_layer_egress_init",
        "$BUILD_DIR/mongo/util/version_impl",
    ],
)

env.CppLibfuzzerTest(
    target="op_msg_fuzzer",
    source=[
        "op_msg_fuzzer.cpp",
    ],
    LIBDEPS=[
        "op_msg_fuzzer_fixture",
        "server_base",
    ],
)

env.Benchmark(
    target="commands_bm",
    source=[
        "commands_bm.cpp",
    ],
    LIBDEPS=[
        "server_base",
    ],
    CONSOLIDATED_TARGET="first_half_bm",
)

env.Benchmark(
    target="operation_cpu_timer_bm",
    source=[],
    LIBDEPS=[],
)

env.Benchmark(
    target="d_concurrency_bm",
    source=[
        "d_concurrency_bm.cpp",
    ],
    LIBDEPS=[
        "service_context_non_d",
        "shard_role_api",
    ],
)

env.CppUnitTest(
    target="service_entry_point_shard_role_test",
    source=[],
    LIBDEPS=[],
    PROVE_ALIASES=["server-programmability"],
)

env.Benchmark(
    target="service_entry_point_shard_role_bm",
    source=[
        "service_entry_point_shard_role_bm.cpp",
    ],
    LIBDEPS=[
        "$BUILD_DIR/mongo/db/index_builds/index_builds_coordinator_mongod",
        "$BUILD_DIR/mongo/util/periodic_runner_factory",
        "auth/authserver",
        "catalog/catalog_impl",
        "commands/standalone",
        "dbdirectclient",
        "mongod_options_init",
        "op_observer/op_observer",
        "read_write_concern_defaults_mock",
        "repl/replmocks",
        "repl/storage_interface_impl",
        "s/sharding_runtime_d",
        "server_base",
        "service_context_d",
        "storage/storage_control",
        "storage/wiredtiger/storage_wiredtiger",
        "vector_clock_mongod",
    ],
)

env.Benchmark(
    target="namespace_string_bm",
    source=[],
    LIBDEPS=[],
)

env.Benchmark(
    target="collection_acquisition_bm",
    source=[
        "collection_acquisition_bm.cpp",
    ],
    LIBDEPS=[
        "repl/replmocks",
        "service_context_d_test_fixture",
        "shard_role",
        "shard_role_api",
    ],
)

env.CppUnitTest(
    target="server_lifecycle_monitor_test",
    source=[],
    LIBDEPS=[],
)

env.CppUnitTest(
    target="op_msg_fuzzer_fixture_test",
    source=[],
    LIBDEPS=[],
)
